package de.zainodis.triplog.data.persistence;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

import de.zainodis.triplog.data.DiaryEntry;
import de.zainodis.triplog.data.LocationEntry;
import de.zainodis.triplog.data.TripEntry;
import de.zainodis.triplog.data.WeatherEntry;

/**
 * Handles basic database setup and upgrades.
 * Created by fzarrai on 8/20/2014.
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
    private static final String TAG = "DatabaseHelper";
    /**
     * Any time database objects change, this version MUST be incremented
     */
    private static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "triplog.db";

    // All the Dao objects used in Triplog
    private ITripDao tripDao = null;
    private ILocationDao locationDao = null;
    private IWeatherDao weatherDao = null;
    private IDiaryDao diaryDao = null;


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public DatabaseHelper(Context context, String databaseName, int databaseVersion) {
        super(context, databaseName, null, databaseVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        Log.i(TAG, "Creating tables for " + DATABASE_NAME);
        try {
            TableUtils.createTable(connectionSource, TripEntry.class);
            TableUtils.createTable(connectionSource, LocationEntry.class);
            TableUtils.createTable(connectionSource, WeatherEntry.class);
            TableUtils.createTable(connectionSource, DiaryEntry.class);
        } catch (SQLException e) {
            Log.e(TAG, "Failed to create tables.", e);
            throw new RuntimeException("Failed to create SQLite tables.", e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i2) {
        //Make sure to put table upgrade code here!
    }

    /**
     * ******************************
     * Rudimentary methods for accessing DAOs. Specific DAO operations are
     * implemented in their corresponding dao classes or associated persister-classes.
     * *******************************
     */

    public ITripDao getTripDao() throws SQLException {
        if (tripDao == null) {
            tripDao = getDao(TripEntry.class);
        }
        return tripDao;
    }

    public IDiaryDao getDiaryDao() throws SQLException {
        if (diaryDao == null) {
            diaryDao = getDao(DiaryEntry.class);
        }
        return diaryDao;
    }

    public IWeatherDao getWeatherDao() throws SQLException {
        if (weatherDao == null) {
            weatherDao = getDao(WeatherEntry.class);
        }
        return weatherDao;
    }

    public ILocationDao getLocationDao() throws SQLException {
        if (locationDao == null) {
            locationDao = getDao(LocationEntry.class);
        }
        return locationDao;
    }
}
